﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace MODULE_QUANTRIHETHONG
{
    public partial class frm_thamso : Form
    {
        hethongDataContext context = cls_quantrihethong.getcontext();
        public frm_thamso()
        {
            InitializeComponent();
        }

        private void btFont_Click(object sender, EventArgs e)
        {
            

            
        }

        private void frm_thamso_Load(object sender, EventArgs e)
        {
            IEnumerable<thamso> thamsos = from c in context.thamsos select c;
            for (int i = 0; i < thamsos.Count(); i++)
            {
                thamso thamso=thamsos.ElementAt(i);
                if (thamso.tenthamso == constant.TRONGLUONGTRENBAO)
                {
                    this.txttrongluongtrenbao.Text = thamso.giatri;
                }
                if (thamso.tenthamso == constant.TRULUONGBOTCAONHAT)
                {
                    this.txtmax.Text = thamso.giatri;
                }
                if (thamso.tenthamso == constant.TRULUONGBOTTRUNGBINH)
                {
                    this.txtavg.Text = thamso.giatri;
                }
                if (thamso.tenthamso == constant.TRULUONGBOTTHAPNHAT)
                {
                    this.txtmin.Text = thamso.giatri;
                }
                if (thamso.tenthamso == constant.LOAIHINHCAN)
                {
                    if (thamso.giatri == constant.CANBANGTAY)
                    {
                        this.ckdcanbangtay.Checked = true;
                        
                    }
                    if (thamso.giatri == constant.CANTUDONG)
                    {
                        this.ckdcantudong.Checked = true;
                    }
                }

            }
        }

        private void ckdcantudong_CheckedChanged(object sender, EventArgs e)
        {
            
        }

        private void ckdcanbangtay_CheckedChanged(object sender, EventArgs e)
        {
            
        }

        private void ckdcantudong_Click(object sender, EventArgs e)
        {
            if (this.ckdcantudong.Checked == true)
            {
                this.ckdcantudong.Checked = false;
                ckdcanbangtay.Checked = true;
            }
            if (this.ckdcantudong.Checked == false)
            {
                this.ckdcantudong.Checked = true;
                this.ckdcanbangtay.Checked = false;
            }
        }

        private void ckdcanbangtay_Click(object sender, EventArgs e)
        {
            if (this.ckdcanbangtay.Checked == true)
            {
                this.ckdcanbangtay.Checked = false;
                ckdcantudong.Checked = true;
            }
            if (this.ckdcanbangtay.Checked == false)
            {
                this.ckdcanbangtay.Checked = true;
                this.ckdcantudong.Checked = false;
            }
        }

        private void btSave_Click(object sender, EventArgs e)
        {
            try
            {
                double trongluongtrenbao;
                try
                {
                    trongluongtrenbao = double.Parse(txttrongluongtrenbao.Text.ToString());
                }
                catch (Exception ex)
                {
                    trongluongtrenbao = 0;
                }
                IEnumerable<thamso> thamsos = from c in context.thamsos select c;
                bool flag = false;
                for (int i = 0; i < thamsos.Count(); i++)
                {
                    thamso thamso = thamsos.ElementAt(i);
                    if (thamso.tenthamso == constant.TRONGLUONGTRENBAO)
                    {
                        thamso.giatri = trongluongtrenbao.ToString();
                        context.SubmitChanges();
                        flag = true;
                    }
                }
                if (flag == false)
                {
                    thamso thamso = new thamso();
                    thamso.tenthamso = constant.TRONGLUONGTRENBAO;
                    thamso.giatri = trongluongtrenbao.ToString();
                    context.thamsos.InsertOnSubmit(thamso);
                    context.SubmitChanges();

                }


                double max;
                try
                {
                    max = double.Parse(this.txtmax.Text.ToString());
                }
                catch (Exception ex)
                {
                    max = 0;
                }

                flag = false;
                for (int i = 0; i < thamsos.Count(); i++)
                {
                    thamso thamso = thamsos.ElementAt(i);
                    if (thamso.tenthamso == constant.TRULUONGBOTCAONHAT)
                    {
                        thamso.giatri = max.ToString();
                        context.SubmitChanges();
                        flag = true;
                    }
                }
                if (flag == false)
                {
                    thamso thamso = new thamso();
                    thamso.tenthamso = constant.TRULUONGBOTCAONHAT;
                    thamso.giatri = max.ToString();
                    context.thamsos.InsertOnSubmit(thamso);
                    context.SubmitChanges();

                }
                double avg;
                try
                {
                    avg = double.Parse(this.txtavg.Text.ToString());
                }
                catch (Exception ex)
                {
                    avg = 0;
                }

                flag = false;
                for (int i = 0; i < thamsos.Count(); i++)
                {
                    thamso thamso = thamsos.ElementAt(i);
                    if (thamso.tenthamso == constant.TRULUONGBOTTRUNGBINH)
                    {
                        thamso.giatri = avg.ToString();
                        context.SubmitChanges();
                        flag = true;
                    }
                }
                if (flag == false)
                {
                    thamso thamso = new thamso();
                    thamso.tenthamso = constant.TRULUONGBOTTRUNGBINH;
                    thamso.giatri = avg.ToString();
                    context.thamsos.InsertOnSubmit(thamso);
                    context.SubmitChanges();

                }

                double min;
                try
                {
                    min = double.Parse(this.txtmin.Text.ToString());
                }
                catch (Exception ex)
                {
                    min = 0;
                }

                flag = false;
                for (int i = 0; i < thamsos.Count(); i++)
                {
                    thamso thamso = thamsos.ElementAt(i);
                    if (thamso.tenthamso == constant.TRULUONGBOTTHAPNHAT)
                    {
                        thamso.giatri = min.ToString();
                        context.SubmitChanges();
                        flag = true;
                    }
                }
                if (flag == false)
                {
                    thamso thamso = new thamso();
                    thamso.tenthamso = constant.TRULUONGBOTTHAPNHAT;
                    thamso.giatri = min.ToString();
                    context.thamsos.InsertOnSubmit(thamso);
                    context.SubmitChanges();

                }
                if (ckdcanbangtay.Checked == true)
                {
                    flag = false;
                    for (int i = 0; i < thamsos.Count(); i++)
                    {
                        thamso thamso = thamsos.ElementAt(i);
                        if (thamso.tenthamso == constant.LOAIHINHCAN)
                        {
                            thamso.giatri = constant.CANBANGTAY;
                            context.SubmitChanges();
                            flag = true;
                        }
                    }
                    if (flag == false)
                    {
                        thamso thamso = new thamso();
                        thamso.tenthamso = constant.LOAIHINHCAN;
                        thamso.giatri = constant.CANBANGTAY;
                        context.thamsos.InsertOnSubmit(thamso);
                        context.SubmitChanges();

                    }
                }
                if (ckdcantudong.Checked == true)
                {
                    flag = false;
                    for (int i = 0; i < thamsos.Count(); i++)
                    {
                        thamso thamso = thamsos.ElementAt(i);
                        if (thamso.tenthamso == constant.LOAIHINHCAN)
                        {
                            thamso.giatri = constant.CANTUDONG;
                            context.SubmitChanges();
                            flag = true;
                        }
                    }
                    if (flag == false)
                    {
                        thamso thamso = new thamso();
                        thamso.tenthamso = constant.LOAIHINHCAN;
                        thamso.giatri = constant.CANTUDONG;
                        context.thamsos.InsertOnSubmit(thamso);
                        context.SubmitChanges();

                    }

                }
                MessageBox.Show("Lưu thành công");
            }
            catch (Exception ex)
            {
                MessageBox.Show("Không lưu đuợc.Lỗi " + ex.Message);
                MessageBox.Show(ex.Message);
            }


        }
    }
}
